package com.party.core.service.gift.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.UUIDUtils;
import com.party.core.dao.read.gift.GiftTargetReadDao;
import com.party.core.dao.write.gift.GiftTargetWriteDao;
import com.party.core.model.gift.GiftTarget;
import com.party.core.model.gift.GiftTargetListOutput;
import com.party.core.service.gift.IGiftTargetService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 礼包和目标
 * @Date:Created in 2017/11/15 0015 上午 10:35.
 */
@Service
public class GiftTargetService implements IGiftTargetService {
    @Autowired
    private GiftTargetReadDao giftTargetReadDao;
    @Autowired
    private GiftTargetWriteDao giftTargetWriteDao;

    @Override
    public String insert(GiftTarget t) {
        String uuid = UUIDUtils.generateRandomUUID();
        if (Strings.isNullOrEmpty(t.getId())) {//如果业务层没有指定主键id
            t.setId(uuid);
        }
        boolean result = giftTargetWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(GiftTarget t) {
        return giftTargetWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftTargetWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftTargetWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftTargetWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftTargetWriteDao.batchDelete(ids);
    }

    @Override
    public GiftTarget get(String id) {
        return giftTargetReadDao.get(id);
    }

    @Override
    public List<GiftTarget> listPage(GiftTarget t, Page page) {
        return giftTargetReadDao.listPage(t, page);
    }

    @Override
    public List<GiftTarget> list(GiftTarget t) {
        return giftTargetReadDao.listPage(t, null);
    }

    @Override
    public List<GiftTarget> batchList(Set<String> ids, GiftTarget t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return giftTargetReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public List<GiftTarget> findByGiftId(String giftId) {
        GiftTarget giftTarget = new GiftTarget();
        giftTarget.setGiftId(giftId);
        return giftTargetReadDao.listPage(giftTarget, null);
    }

    @Override
    public List<GiftTarget> findByTargetId(String targetId) {
        GiftTarget giftTarget = new GiftTarget();
        giftTarget.setTargetId(targetId);
        return giftTargetReadDao.listPage(giftTarget, null);
    }

    @Override
    public GiftTarget getUnique(GiftTarget giftTarget) {
        return giftTargetReadDao.getUnique(giftTarget);
    }

    @Override
    public void deleteByGiftId(String giftId) {
        giftTargetWriteDao.deleteByGiftId(giftId);
    }

    @Override
    public List<GiftTargetListOutput> webListPage(GiftTargetListOutput giftTarget, Map<String, Object> params, Page page) {
        return giftTargetReadDao.webListPage(giftTarget, params, page);
    }

    @Override
    public Integer getCountByGiftId(String giftId) {
        return giftTargetReadDao.getCountByGiftId(giftId);
    }

    @Override
    public void deleteByUnique(GiftTarget giftTarget) {
        giftTargetWriteDao.deleteByUnique(giftTarget);
    }

    @Override
    public void deleteByTargetId(String targetId) {
        giftTargetWriteDao.deleteByTargetId(targetId);
    }
}
